package com.emptyMe.user.controller;

import com.emptyMe.basic.utils.AjaxResult;
import com.emptyMe.org.domain.Goods;
import com.emptyMe.user.domain.User;
import com.emptyMe.user.domain.UserDTO;
import com.emptyMe.user.query.UserQuery;
import com.emptyMe.user.service.IUserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细api描述")
public class UserController {

    @Autowired
    private IUserService service;

    @GetMapping("/getCode")
    @ApiOperation(value = "获取验证码")
    public AjaxResult getCode(String phone) {
        return service.getCode(phone);
    }

    @PostMapping("/registerByPhone")
    @ApiOperation(value = "通过手机号注册")
    public AjaxResult registerByPhone(@RequestBody UserQuery query){
        return service.registerByPhone(query);
    }

    @PostMapping("logout")
    @ApiOperation(value = "退出登录")
    public AjaxResult logout(){
        return service.logout();
    }

    @GetMapping("/getByUsername")
    public AjaxResult getByUsername(String username) {
        return service.findByUsername(username);
    }

    @PostMapping("account")  // account:账户
    @ApiOperation(value = "登录功能实现")
    public AjaxResult login(@RequestBody UserQuery query) {
        try {
            UserDTO dto = service.login(query);
            return AjaxResult.me(dto);
        } catch (Exception e) {
            return AjaxResult.me(false, e.getMessage());
        }
    }

    @PostMapping("/edit")  // edit修改
    @ApiOperation(value = "修改信息功能实现")
    public AjaxResult Edit(@RequestBody User user) {
        System.out.println(user);
//        return AjaxResult.me(user);;
        try {
            service.edit(user);
            return AjaxResult.me();
        } catch (Exception e) {
            return AjaxResult.me(false, e.getMessage());
        }
    }

    @PostMapping("joinUs")  // 加入我们
    @ApiOperation(value = "用户加入功能实现")
    public AjaxResult joinUs(@RequestBody User user) {
        try {
            service.joinUs(user);
            return AjaxResult.me();
        } catch (Exception e) {
            return AjaxResult.me(false, e.getMessage());
        }
    }

    @PostMapping("findUser")  // 加入我们
    @ApiOperation(value = "查询所有用户")
    public AjaxResult findUser(@RequestBody UserQuery query) {
        try {
            PageInfo<User> user = service.findUser(query);
            return AjaxResult.me(user);
        } catch (Exception e) {
            return AjaxResult.me(false, e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除用户")
    public AjaxResult del(@PathVariable List<Long> id){
        try {
            service.delUser(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"删除失败"+e.getCause());
        }
    }

    @DeleteMapping("/delUser")
//    @ApiOperation(value = "批量删除")
    public AjaxResult delAll(@RequestBody List<Goods> goods){
        try {
            ArrayList<Long> longs = new ArrayList<>();
            for (Goods good : goods) {
                System.out.println(good.getId());
                longs.add(good.getId());
            }
            service.delUser(longs);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"删除失败"+e.getCause());
        }
    }
}
